package com.atguigu.tacket;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


class Demo {

    private int number = 50;
    Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        Demo demo = new Demo();

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                demo.Resource();
//            }
//        },"A").start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                demo.Resource();
//            }
//        },"b").start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                demo.Resource();
//            }
//        },"c").start();

        new Thread(() -> {demo.Resource();}, "A").start();
        new Thread(() -> {demo.Resource();}, "B").start();
        new Thread(() -> {demo.Resource();}, "C").start();


    }

    public void Resource() {
        {
            lock.lock();
            try {
                while (number > 0) {
                    System.out.println(Thread.currentThread().getName() + "正在卖：" + (number) + "，还剩：" + (number--));
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
